find_package(Qt5 REQUIRED COMPONENTS LinguistTools)
set_directory_properties(PROPERTIES CLEAN_NO_CUSTOM 1)

file(MAKE_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}/../translations)
find_program(QT_LUPDATE_EXECUTABLE lupdate)
find_program(QT_LRELEASE_EXECUTABLE lrelease)
# QT4_WRAP_TS(lupdate_outputs lrelease_outputs prefix lang1 lang2 ... )
MACRO (QT5_WRAP_TS lupdate_outputs lrelease_outputs prefix)
    file(GLOB_RECURSE srcs
         "${CMAKE_CURRENT_SOURCE_DIR}/../*.h"
         "${CMAKE_CURRENT_SOURCE_DIR}/../*.cpp"
         "${CMAKE_CURRENT_SOURCE_DIR}/../*.ui")
    FOREACH (it ${ARGN})
      SET(tsfile ${CMAKE_CURRENT_SOURCE_DIR}/${prefix}_${it}.ts)
      ADD_CUSTOM_COMMAND(OUTPUT ${tsfile} POST_BUILD
        COMMAND ${QT_LUPDATE_EXECUTABLE} -recursive ${CMAKE_CURRENT_SOURCE_DIR}/.. -ts ${tsfile}
        DEPENDS ${srcs}
      )

      SET(qmfile ${EXECUTABLE_OUTPUT_PATH}/../translations/${prefix}_${it}.qm)
      ADD_CUSTOM_COMMAND(OUTPUT ${qmfile} POST_BUILD
        COMMAND ${QT_LRELEASE_EXECUTABLE} ${tsfile} -qm ${qmfile}
        DEPENDS ${tsfile}
      )

      SET(${lupdate_outputs} ${${lupdate_outputs}} ${tsfile})
      SET(${lrelease_outputs} ${${lrelease_outputs}} ${qmfile})
    ENDFOREACH(it)
ENDMACRO (QT5_WRAP_TS)

# Create our translation files.
QT5_WRAP_TS(lupdate_outputs lrelease_outputs
  qlpmsp
  zh_CN en ar bg ca cs da de es fi fr gd he hu it ja ko lv pl ru sk uk zh_TW
)
set(LANG_SRCS ${lrelease_outputs} PARENT_SCOPE)

ADD_CUSTOM_TARGET(translations
  DEPENDS ${lrelease_outputs}
)

install(FILES ${lrelease_outputs} DESTINATION translations)
